package visualization;

import static org.junit.Assert.*;

import model.*;
import org.junit.Test;

import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.DelegateTree;
import edu.uci.ics.jung.graph.Graph;


public class ModelToGraphTest {

	private static DelegateTree<Element, Float> firstLevel_dt;
	private static DelegateTree<Element, Float> secondLevel_dt;

	public static DelegateTree<Element, Float> getFirstLevel_delegateTree(){
		if (firstLevel_dt==null){
			SysPackage root = new SysPackage("root");
			SysPackage p1 = new SysPackage("package1");
			SysPackage p2 = new SysPackage("package2");
			SysPackage p3 = new SysPackage("package3");
			SysPackage p4 = new SysPackage("package4");
			SysClass c1 = new SysClass("Class1");
			SysClass c2 = new SysClass("Class2");
			SysClass c3 = new SysClass("Class3");

			root.add(p1);
			root.add(p2);
			root.add(p3);
			root.add(p4);
			root.add(c1);
			root.add(c2);
			root.add(c3);

			firstLevel_dt = new DelegateTree<Element, Float>();
			firstLevel_dt.addVertex(root);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, p1);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, p2);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, p3);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, p4);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, c1);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, c2);
			firstLevel_dt.addChild((float)firstLevel_dt.getEdgeCount(), root, c3);		
			//First level done
		}
		return firstLevel_dt;
	}

	public static DelegateTree<Element, Float> getSecondLevel_delegateTree(){
		if(secondLevel_dt==null){
			SysPackage root = new SysPackage("root");
			SysPackage p1 = new SysPackage("package1");
			SysPackage p2 = new SysPackage("package2");
			SysPackage p3 = new SysPackage("package3");
			SysPackage p4 = new SysPackage("package4");
			SysClass c1 = new SysClass("Class1");
			SysClass c2 = new SysClass("Class2");
			SysClass c3 = new SysClass("Class3");

			root.add(p1);
			root.add(p2);
			root.add(p3);
			root.add(p4);
			root.add(c1);
			root.add(c2);
			root.add(c3);

			secondLevel_dt = new DelegateTree<Element, Float>();
			secondLevel_dt.addVertex(root);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, p1);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, p2);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, p3);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, p4);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, c1);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, c2);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), root, c3);	

			SysPackage p5 = new SysPackage("package1");
			SysPackage p6 = new SysPackage("package2");
			SysPackage p7 = new SysPackage("package3");
			SysPackage p8 = new SysPackage("package4");
			SysClass c4 = new SysClass("Class4");
			SysClass c5 = new SysClass("Class5");
			SysClass c6 = new SysClass("Class6");
			SysClass c7 = new SysClass("Class7");
			
			p1.add(p5);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), p1, p5);
			p5.add(p6);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), p5, p6);
			p6.add(c4);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), p6, c4);
			p2.add(p7);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), p2, p7);
			p3.add(p8);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), p3, p8);
			c1.add(c5);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), c1, c5);
			c4.add(c6);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), c4, c6);
			c6.add(c7);
			secondLevel_dt.addChild((float)secondLevel_dt.getEdgeCount(), c6, c7);
			
		}
		return secondLevel_dt;
	}

	/**checks only the first children*/
	@Test
	public void justFirstChildren(){
		DelegateTree<Element, Float> dt = getFirstLevel_delegateTree();
		DelegateTree<Element, Float> autoDT = ModelToGraph.delegateTree_justFirstChildren(dt.getRoot());
		//tests
		assertTrue(autoDT.getEdgeCount()==dt.getEdgeCount());
		for(Element vertex : dt.getVertices())
			assertTrue(autoDT.containsVertex(vertex));
		for(Element vertex : autoDT.getVertices())
			assertTrue(dt.containsVertex(vertex));
	}


	@Test
	public void secondLevelChildrenTest(){
		DelegateTree<Element, Float> dt = getSecondLevel_delegateTree();
		DelegateTree<Element, Float> autoDT = ModelToGraph.delegateTree_fullGraph(dt.getRoot());

		assertTrue(autoDT.getEdgeCount()==dt.getEdgeCount());
		for(Element vertex : dt.getVertices()){
			assertTrue(autoDT.containsVertex(vertex));
		}
		for(Element vertex : autoDT.getVertices()){
			assertTrue(dt.containsVertex(vertex));
		}
	}
	
	
	@SuppressWarnings("rawtypes")
	@Test
	public void testEspecialEdges(){
		SysRoot r = new SysRoot();
		Graph g = new DelegateForest();
		SysPackage p = new SysPackage("package");
		SysClass c = new SysClass("Class");
		SysMethod m = new SysMethod(false, "Method1", "V", "public");
		c.add(m);
		p.add(c);
		r.add(p);
		m.add(m);
		assertTrue(ModelToGraph.getEspecialEdges(r, g).size()==1);
	}

}
